
var register = toContextPath('/student/student_register.action');
var loginHtml = toContextPath('/StuPages/login.html');

$(function(){
	initRegister();
});
function validateEmail(email) {
	//var reg = "/^[a-zA-Z]{2}[a-zA-Z0-9]{1,6}@mail.ustc.edu.cn$/";
	if (email.match(/^[a-zA-Z]{2}[a-zA-Z0-9]{1,6}@mail.ustc.edu.cn$/) != null) {
        $("#emailHint").css("display", "none");
        $("#emailOk").css("display", "inline");
    }
}

function validatePwd(pwd) {
    if (pwd.match(/^[a-zA-Z0-9]{6,12}$/) != null) {
        $("#pwdOk").css("display", "inline");
    }
}
function Account(email, pwd) {
    this.email = email;
    this.pwd = pwd;
}
function submitClick() {
    var email=$("#userEmail").value;
    var pwd = $("#userPwd").value;
    var account = new Account(email, pwd);
    alert(account.email+account.pwd);
}

function initRegister() {
	$('#submitReg').click(function(){
		$('#registerForm').form({
			url: register,
			success: function(result) {
				var data = $.parseJSON(result);
				if(data.success) {
					alert("register success");
					window.location.href = loginHtml;
					
				}		
				else
					alert('register failed!');
			}
		});
	});
}

function form2JSON(form) {
	var jsonobj = {};
	var classNo = getSelected(form);
	//var classNo = jQuery('input[name=class]:checked' ).val() ;
	var stuEmail = $('#stuEmail').text();
	var stuName = $('#stuName').text();
	var stuPwd = $('#inputPassword3').text();
	jsonobj = $.parseJSON({"stuEmail":stuEmail, "stuName":stuName, 
					"stuPassword":stuPwd, "class":classNo});
    return jsonobj;
}

function getSelected(form) {
	var val;
	var radios = form.elements[name];
    
    for (var i=0, len=radios.length; i<len; i++) {
        if ( radios[i].checked == true ) {
            val = radios[i].value;
            break;
        }
    }  
    return val;
}

function ajaxSumbit(form, fn) {
	var json = form2JSON(form);
	$.ajax({
        url: register,
        type: form.method,
        data: json,
        success: fn
    });
}